package interfaces.menu;

import interfaces.GUISource;
import interfaces.ImageUpdater;
import interfaces.language.LanguageButton;
import interfaces.menu.joinGame.JoinGameContent;
import interfaces.menu.options.OptionsMenu;
import interfaces.superWidgets.InterfaceButton;
import interfaces.superWidgets.InterfaceElement;
import interfaces.superWidgets.InterfaceLabel;
import interfaces.superWidgets.tabs.TabSwitchContent;
import fileHandling.language.Lang;
import fileHandling.language.interfaces.MenuText;
import gameStates.absGamesStates.gui.AbsMenuState;

import org.fenggui.StatefullWidget;
import org.fenggui.appearance.LabelAppearance;
import org.fenggui.binding.render.ImageFont;
import org.fenggui.decorator.background.PlainBackground;
import org.fenggui.event.ButtonPressedEvent;
import org.fenggui.event.IButtonPressedListener;
import org.fenggui.util.Color;
import org.fenggui.util.Dimension;
import org.fenggui.util.Point;

import update.Updater;

import main.InitGame;

public class Menu extends InterfaceElement implements AbsMenu {
	
	public static final String LOGO_FILE = "data/textures/logo.png";
	public static final String LOGO_WIDE_FILE = "data/textures/logo_wide.png";
	
	public static final Color DEFAULT_BG_COLOR = Color.GRAY;
	public static final Color CONTENT_BG_COLOR = Color.LIGHT_GRAY;
	
	private ImageUpdater logoContent;
	
	private TabSwitchContent tabSwitchContent;
	
	private LanguageButton continueButton, leaveGameButton;
	private boolean continueButtonSet;
	private AbsMenuState menuState;
	
	private JoinGameContent joinGameContent;
	private CreateGameContent createGameContent;
	private OptionsMenu optionsContent;
		
	private Point menuContentPos;
	private Dimension menuContentSize;
	
	private int border;
	private ImageFont tabFont;
	
	public Menu(AbsMenuState menuState) {
		super(menuState);
		this.menuState = menuState;
		
		border = getHeight() / 22;
		int maxWidth = getWidth() - (2 * border);
		
		int logoHeight = getHeight() / 7;
		int logoWidth = getWidth() - (2 * border);
		
		float ratio = logoWidth / (float)logoHeight;
		String logoFile = ratio > 9 ? LOGO_WIDE_FILE : LOGO_FILE;
		
		int logoY = getHeight() - logoHeight;
		int logoX = (getWidth() / 2) - (logoWidth / 2);
		logoContent = new ImageUpdater(logoFile, logoX, logoY, logoWidth, logoHeight);
		addWidget(logoContent);
		
//		ImageFont logoFont = GUISource.createFont(14);
//		logoLabel = new InterfaceLabel("Infinite Wars", logoFont);
//		logoLabel.setHeight(logoFont.getHeight());
//		logoLabel.setXY((getWidth() / 2) - (logoLabel.getWidth() / 2), (logoHeight / 2) - (logoLabel.getHeight() / 2) + logoY);
//		GUISource.setTextColor(logoLabel, Color.BLUE);
//		addWidget(logoLabel);
		
		int tabSwitchHeight = logoY - border;
		tabFont = GUISource.middleFont;
		
		int x = 0;
		int y = 0;
		menuContentPos = new Point(x, y);
		
		tabSwitchContent = new TabSwitchContent(maxWidth, tabSwitchHeight, tabFont);
		tabSwitchContent.setXY(border, border);
		addWidget(tabSwitchContent);
		
		menuContentSize = tabSwitchContent.getSwitchContentSize();
		
		createMenuContents();
		createTabButtons();
		
		createButtons();
		createVersionLabel();
		
		tabSwitchContent.switchContent(createGameContent);
		
		menuState.getGUIDisplay().layout();
	}
	
	private void createMenuContents() {
		joinGameContent = new JoinGameContent(this);
		createGameContent = new CreateGameContent(this);
		optionsContent = new OptionsMenu(this);
	}
	
	private void createTabButtons() {
		addTabButton(MenuText.Join_Game, joinGameContent);
		addTabButton(MenuText.Create_Game, createGameContent);
		addTabButton(MenuText.Options, optionsContent);
		
		LanguageButton logoutButton = getNewTabButton(MenuText.Logout);
		tabSwitchContent.addTabButton(logoutButton);
		logoutButton.addButtonPressedListener(new IButtonPressedListener() {
			@Override
			public void buttonPressed(ButtonPressedEvent e) {
				InitGame.get().logout();
			}
		});
		
		LanguageButton quitButton = getNewTabButton(MenuText.Quit);
		tabSwitchContent.addTabButton(quitButton);
		quitButton.addButtonPressedListener(new IButtonPressedListener() {
			@Override
			public void buttonPressed(ButtonPressedEvent e) {
				InitGame.get().exit();
			}
		});
	}
	
	private void addTabButton(Lang text, MenuContent content) {
		LanguageButton button = getNewTabButton(text);
		tabSwitchContent.addSwitchContent(button, content);
	}
	
	private LanguageButton getNewTabButton(Lang text) {
		LanguageButton button = new LanguageButton(text, tabFont);
		return button;
	}
	
	private void createVersionLabel() {
		ImageFont font = GUISource.middleFont;
		String version = Updater.getThisVersion();
		String labelText = "Version: " + version;
		int width = font.getWidth(labelText);
		
		InterfaceLabel versionLabel = new InterfaceLabel(labelText, font);
		versionLabel.setX(getWidth() - width - font.getWidth("W"));
		versionLabel.setSize(width, getHeight() / 20);
		GUISource.setTextColor(versionLabel, Color.WHITE);
		addWidget(versionLabel);
	}
	
	private void createButtons() {
		ImageFont buttonFont = GUISource.middleFont;
		int width = getWidth() / 5;
		int height = buttonFont.getHeight() / 3 * 4;
		int y = getHeight() - height;
		
		continueButton = new LanguageButton(MenuText.Continue, buttonFont);
		continueButton.setSize(width, height);
		continueButton.setXY(border, y);
		setupTopButton(continueButton);
		continueButton.addButtonPressedListener(new IButtonPressedListener() {
			@Override
			public void buttonPressed(ButtonPressedEvent e) {
				InitGame.get().changeIngame(true);
			}
		});
		
		leaveGameButton = new LanguageButton(MenuText.Leave_Game, buttonFont);
		leaveGameButton.setSize(width, height);
		leaveGameButton.setXY(continueButton.getX() + continueButton.getWidth(), y);
		setupTopButton(leaveGameButton);
		leaveGameButton.addButtonPressedListener(new IButtonPressedListener() {
			@Override
			public void buttonPressed(ButtonPressedEvent e) {
				addWidget(new LeaveGameWindow(Menu.this));
			}
		});
	}
	
	private void setupTopButton(InterfaceButton button) {
		LabelAppearance app = button.getAppearance();
		app.removeAll();
		app.clearSpacings();
		
		app.add(StatefullWidget.STATE_DEFAULT, new PlainBackground(Color.GRAY));
		app.add(StatefullWidget.STATE_HOVERED, new PlainBackground(Color.BLUE));
		
		app.setEnabled(StatefullWidget.STATE_DEFAULT, true);
		app.setEnabled(StatefullWidget.STATE_HOVERED, false);
		
		GUISource.setTextColor(button, Color.WHITE);
	}
	
	@Override
	public void enableContinueButton(boolean enable) {
		if(enable && !continueButtonSet) addWidget(continueButton);
		else if(!enable && continueButtonSet) removeWidget(continueButton);
		
		continueButtonSet = enable;
	}
	
	@Override
	public void setGameStarted(boolean gameStarted) {
		if(gameStarted) addWidget(leaveGameButton);
		else removeWidget(leaveGameButton);
	}
	
	@Override
	public void updateMoney(int newMoney) {}
	
	@Override
	public boolean gameInitialized() { return menuState.gameInitialized(); }
	
	@Override
	public AbsMenuState getMenuState() { return menuState; }
	
	@Override
	public Dimension getMenuContentSize() { return menuContentSize; }
	
	@Override
	public Point getMenuContentPosition() { return menuContentPos; }
}